class: center, middle, inverse, title-slide .title[ # Module 1: The Basics ] .subtitle[ ## Gitting and Subsetting ] .author[ ### Dr. Christopher Kenaley ] .institute[ ### Boston College ] .date[ ### 2025/8/29 ] --- class: inverse, top # Today we'll ..... .pull-left[ - Review Gitting, clone a repo - Subsetting data - `for` loops - Peak under the hood of Module Project 1 ] --- class: inverse, top # Pleas for help .pull-left[ Please accept team and repo invites! ] .pull-right[  ] --- class: inverse, top # Git and RStudio ## Important steps .pull-left[ 1a: [If repo exists] Go to repo on github, copy URL 1b: [If repo doesn't exists] Make a repot on github, copy ] .pull-right[  ] --- class: inverse, top # Git and RStudio ## Important steps .pull-left[ 2: Create an R project + Go to File -> New Project.... + Choose Version Control: + In the "New Project" dialog, select "Version Control". ] .pull-right[  ] --- class: inverse, top # Git and RStudio ## Important steps .pull-left[ 3: Select "Git" as the system - Click **Git**. ] .pull-right[  ] --- class: inverse, top # Git and RStudio ## Important steps .pull-left[ 4: Paste the Repository URL - In **Clone Git Repository**, paste the repo **URL** (copied from GitHub). - Example: `https://github.com/YOUR-ORG/YOUR-REPO.git` ] .pull-right[  ] --- class: inverse, top # Git and RStudio ## Important steps .pull-left[ 5: Name the repo locally - Give your local project a short, clear **Name**. - This becomes the folder name. 6: Choose a destination folder - Click **Browse…** and pick where to put the project. - Tip: keep a top-level folder/directory like `~/bc-3140/` or `~/courses/`. ] .pull-right[  ] --- class: inverse, top # Git and RStudio ## Important steps .pull-left[ 7: Click "Create Project" - RStudio will clone the repo and open the project. - Look for the **Git** pane (upper right: "Commit", "Pull", "Push"). ] .pull-right[  ] --- class: inverse, top # Git and RStudio # You're ready to code 🎉 .pull-left[ - Use the **Git** tab to manage workflow. - *Always* Use **Pull** first to sync download - **Commit** with message to stage make changes. - **Push** to publish. ] .pull-right[  ] --- class: inverse, top # Subsetting ## Goals - Compare **row** and **column** subsetting in **base R** - Show common patterns: conditions, positions, name patterns, etc. - Use the built-in **`iris`** dataset for reproducible examples --- class: inverse, top # Data & Setup ``` r library(tidyverse) #Always! class(iris) ``` ``` ## [1] "data.frame" ``` ``` r iris <- as_tibble(iris) #converts data frame to tibble class(iris) ``` ``` ## [1] "tbl_df" "tbl" "data.frame" ``` ``` r head(iris) ``` ``` ## # A tibble: 6 × 5 ## Sepal.Length Sepal.Width Petal.Length Petal.Width Species ## <dbl> <dbl> <dbl> <dbl> <fct> ## 1 5.1 3.5 1.4 0.2 setosa ## 2 4.9 3 1.4 0.2 setosa ## 3 4.7 3.2 1.3 0.2 setosa ## 4 4.6 3.1 1.5 0.2 setosa ## 5 5 3.6 1.4 0.2 setosa ## 6 5.4 3.9 1.7 0.4 setosa ``` --- class: inverse, top # data frames and tibbles - data frame (`?data.frame`): 2D table, rows x column - tibble (`?tibble`): super-charges 2D table, rows x column ## Isolating rows, columns in 2D classes ``` r # rows iris[1,] ``` ``` ## # A tibble: 1 × 5 ## Sepal.Length Sepal.Width Petal.Length Petal.Width Species ## <dbl> <dbl> <dbl> <dbl> <fct> ## 1 5.1 3.5 1.4 0.2 setosa ``` --- class: inverse, top ## Isolating rows, columns in 2D classes ``` r # multiple rows iris[c(1,3,5),] ``` ``` ## # A tibble: 3 × 5 ## Sepal.Length Sepal.Width Petal.Length Petal.Width Species ## <dbl> <dbl> <dbl> <dbl> <fct> ## 1 5.1 3.5 1.4 0.2 setosa ## 2 4.7 3.2 1.3 0.2 setosa ## 3 5 3.6 1.4 0.2 setosa ``` --- class: inverse, top ## Isolating rows, columns in 2D classes ``` r #columns head(iris[,1]) ``` ``` ## # A tibble: 6 × 1 ## Sepal.Length ## <dbl> ## 1 5.1 ## 2 4.9 ## 3 4.7 ## 4 4.6 ## 5 5 ## 6 5.4 ``` ``` r #columns with $ head(iris$Sepal.Length) ``` ``` ## [1] 5.1 4.9 4.7 4.6 5.0 5.4 ``` --- class: inverse, top ## Isolating rows, columns in 2D classes ``` r #columns with names head(iris[c("Sepal.Length","Sepal.Width")]) ``` ``` ## # A tibble: 6 × 2 ## Sepal.Length Sepal.Width ## <dbl> <dbl> ## 1 5.1 3.5 ## 2 4.9 3 ## 3 4.7 3.2 ## 4 4.6 3.1 ## 5 5 3.6 ## 6 5.4 3.9 ``` --- class: inverse, top # Row Filtering by Condition ### With `[]` and conditions ``` r # Versicolor flowers with Sepal.Length > 6 subset_rows <- iris[iris$Sepal.Length > 6 & iris$Species == "versicolor", ] head(subset_rows) ``` ``` ## # A tibble: 6 × 5 ## Sepal.Length Sepal.Width Petal.Length Petal.Width Species ## <dbl> <dbl> <dbl> <dbl> <fct> ## 1 7 3.2 4.7 1.4 versicolor ## 2 6.4 3.2 4.5 1.5 versicolor ## 3 6.9 3.1 4.9 1.5 versicolor ## 4 6.5 2.8 4.6 1.5 versicolor ## 5 6.3 3.3 4.7 1.6 versicolor ## 6 6.6 2.9 4.6 1.3 versicolor ``` --- class: inverse, top # Row Filtering by Condition ### With `subset()` and conditions ``` r # Using subset() (convenience wrapper) subset_rows2 <- subset(iris, Sepal.Length > 6 & Species == "versicolor") head(subset_rows2) ``` ``` ## # A tibble: 6 × 5 ## Sepal.Length Sepal.Width Petal.Length Petal.Width Species ## <dbl> <dbl> <dbl> <dbl> <fct> ## 1 7 3.2 4.7 1.4 versicolor ## 2 6.4 3.2 4.5 1.5 versicolor ## 3 6.9 3.1 4.9 1.5 versicolor ## 4 6.5 2.8 4.6 1.5 versicolor ## 5 6.3 3.3 4.7 1.6 versicolor ## 6 6.6 2.9 4.6 1.3 versicolor ``` --- class: inverse, top # Row Filtering by Condition ### Allows operations on data of interest ``` r # means vers_sepalL <- iris[iris$Species == "versicolor",]$Sepal.Length set_sepalL <- iris[iris$Species == "setosa",]$Sepal.Length mean(vers_sepalL) ``` ``` ## [1] 5.936 ``` ``` r mean(set_sepalL) ``` ``` ## [1] 5.006 ``` ``` r #same using sapply sapply(list(vers_sepalL,set_sepalL),mean) ``` ``` ## [1] 5.936 5.006 ```